<extend name='./Base' />
<block name='admin-content-body'>
	
	<div class="admin-content-body">
      <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">文章分类</strong> / <small>Categories</small></div>
      </div>
      <hr>
      <div class="am-g">
      <form id="add_cate">
        <div class="am-u-sm-12 am-u-md-3">
          <div class="am-form-group">
              <input type="text" name="category" id="category" class="am-form-field" placeholder="分类名">
          </div>
        </div>
        <div class="am-u-sm-12 am-u-md-9">
          <div class="am-input-group">
          <span class="am-input-group-btn">
            <button type="submit" class="am-btn am-btn-default" type="button">添加</button>
          </span>
          </div>
        </div>
        </form>
      </div>

      <div class="am-g">
        <div class="am-u-sm-12">
            <table class="am-table am-table-striped am-table-hover">
              <thead>
              <tr>
                <th class="table-id">ID</th>
                <th class="table-title">分类名</th>
                <th class="table-date am-hide-sm-only">创建日期</th>
                <th class="table-set">状态(点击切换)</th>
                <th class="table-set">删除</th>
              </tr>
              </thead>
              <tbody>
                <foreach name="categories" item='l'>
              <tr id="{$l['id']}_links">
                <td>{$l['id']}</td>
                <td>{$l['category']}</td>
                <td class="am-hide-sm-only">{$l['create_time']|date='Y-m-d H:i:s',###}</td>
                <td>
                <if condition="$l['status'] eq 1">
                <button class="am-btn am-btn-primary am-btn-xs" onclick="changeCate({$l['id']},this)">
                	可用
                </button>
                <else />
                    <button class="am-btn am-btn-danger am-btn-xs" onclick="changeCate({$l['id']},this)">停用</button>
                </if>
                </td>
                <td>
                  <div class="am-btn-toolbar">
                    <div class="am-btn-group am-btn-group-xs">
                      <button class="am-btn am-btn-danger am-btn-xs am-hide-sm-only" onclick="deletel({$l['id']})"><span class="am-icon-trash-o"></span> 删除</button>
                    </div>
                  </div>
                </td>
              </tr>
             </foreach>
              </tbody>
            </table>
        </div>

      </div>
    </div>

</block>

<block name="js">
	<script type="text/javascript">

		function changeCate(id,obj){
      $.post('__CONTROLLER__/status',{'id':id},function(res){
        if(res.status){
          if(res.data==0){
            //停用
            $(obj).removeClass('am-btn-primary').addClass('am-btn-danger').text('停用');
          }else{
            //可用
            $(obj).removeClass('am-btn-danger').addClass('am-btn-primary').text('可用');
          }
          layer.msg(res.msg);
        }else{
          layer.alert(res.msg,{icon:5});
        }
      });
		}

		function deletel(id) {
	      layer.confirm('确定要删除？不可恢复', {
	        btn: ['确定','取消']
	      },function(){
	        $.post('__CONTROLLER__/delete',{'id':id},function(res){
	          if(res.status){
	            $("#"+id+"_links").remove();
	            layer.msg(res.msg);
	          }else{
	            layer.alert(res.msg,{icon:5});
	          }
	        });
	    },function(){})
	   }
		$("#add_cate").submit(function(){
		    var category=$("#category").val();
		    if($.trim(category)==''){
		      layer.alert('分类不能为空！');
		      return false;
		    }
		    $.post('__CONTROLLER__/add',{'category':category},function(res){
		      if(res.status){
		        // layer.msg(res.msg);
		        location.reload()
		      }else{
		        layer.alert(res.msg);
		      }
		    });
		    return false;
		  })
	</script>
</block>